const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
console.log('webpack---->>>dev', process.env.NODE_ENV);
module.exports = {
  mode: 'development',
  // 这个是vue.config文件这么写
  // resolve: {
  //   extensions: ['.js', '.vue', '.json'],
  //   alias: {
  //     '@': resolve('src')
  //   }
  // },
  resolve: {
    // import的时候可以省略的文件ext
    extensions: ['.js', '.vue', '.json'],
    // 这样@符号就表示项目根目录中src这一层路径
    alias: {
      '@': path.join(__dirname, './src'),
    },
  },
  entry: {
    app: './src/app',
    ch: './src/main',
  },
  output: {
    path: path.resolve(__dirname, 'project'),
    filename: '[name]_[hash:6].js',
  },
  module: {
    rules: [
      { test: /\.vue$/, use: 'vue-loader' },
      { test: /\.css$/, use: ['style-loader', 'css-loader'] },
      {
        test: /\.jpg|png|gif|jpeg$/,
        use: {
          loader: 'file-loader',
          options: {
            limit: 10240,
            name: '[name]_[hash:3].[ext]',
            outputPath: 'img/',
          },
        },
      },
      {
        test: /\.ttf|eot|svg|woff|woff2$/,
        use: 'url-loader',
      },
      // babel转换es6 presets可设置在这里不用起文件
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: ['@babel/plugin-syntax-dynamic-import'],
          },
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      chunks: ['app'],
      title: 'h5',
    }),
    new CleanWebpackPlugin(),
    new VueLoaderPlugin(),
    // ['@babel/plugin-proposal-class-properties', { loose: true }],
  ],
  devServer: {
    compress: true,
    port: 9000,
    proxy: {
      // '/api': {
      //     target: 'http://localhost:8090',
      //     // 设置域名时必须加上
      //     changeOrigin: true,
      //     // 重写路径
      //     pathRewrite: {
      //     '^/api': '',
      //     },
      //     secure: true,
      // },
      '/api': {
        target: 'http://192.168.1.232/',
        changeOrigin: true,
        // pathRewrite: {
        //   '^/api': '',
        // },
      },
    },
  },
};
